<?php
namespace Home\Controller;
use Think\Controller;
class KnowledgeController extends CommnController{
    public function __construct(){
        parent::__construct();
    }

    /**
     * 修改问题
     */
    public function editknowledge(){
        if (empty($_POST)) {
           $this->data= M('knowledge', '', 'KEFU')->where(['id'=>I('id')])->field('ctime',true)->find();
            $this->display();
        } else {
            $img=I('imgurl');
            $imgurl='';
            if (!empty($img)) {
                $upload = new \Admin\Logic\UploadPicLogic();        //实例化 上传图片
                $picarray = explode('~', $img);
                $url="kefu/knowledge/";
                foreach ($picarray as $k1 => $v1) {
                    $returnimg = $upload->LoadIgoodimg($v1,$url);
                    if ($returnimg['status'] == '1') {
                        if($k1==0){
                            $imgurl=$returnimg['pic'];
                        }else{
                            $imgurl=$imgurl.','. $returnimg['pic'];
                        }
                    } else {
                        $this->ajaxReturn(array('state' => '2','result'=>'图片上传失败'), 'json');
                    }
                }
            }
            $data=I('post.');
            $data['img']=$imgurl;
            unset($data['imgurl']);
            if( M('knowledge', '', 'KEFU')->save($data)){
                $this->ajaxReturn(array('state' => '1','result'=>'修改成功'), 'json');
            }else{
                $this->ajaxReturn(array('state' => '2','result'=>'修改失败'), 'json');
            }
        }
    }
    /**
     * 添加问题
     */
    public function addknowledge(){
        if (empty($_POST)) {
            $this->display();
        } else {
            $standard = I('standard');
            $similar = I('similar');
            $answer = I('answer');
            $img=I('imgurl');
            $imgurl='';
            if (!empty($img)) {
                $upload = new \Admin\Logic\UploadPicLogic();        //实例化 上传图片
                $picarray = explode('~', $img);
                $url="kefu/knowledge/";
                foreach ($picarray as $k1 => $v1) {
                    $returnimg = $upload->LoadIgoodimg($v1,$url);
                    if ($returnimg['status'] == '1') {
                        if($k1==0){
                            $imgurl=$returnimg['pic'];
                        }else{
                            $imgurl=$imgurl.','. $returnimg['pic'];
                        }
                    } else {
                        $this->ajaxReturn(array('state' => '2'), 'json');
                    }
                }
            }
            $admin_id = $_SESSION['id'];
            $arr = array('type'=>I('type'),'standard_question' => $standard, 'similar_question' => $similar, 'answer' => $answer, 'admin_id' => $admin_id, 'ctime' => time(),'img'=>$imgurl);
            $add = M('knowledge', '', 'KEFU')->add($arr);
            if ($add) {
                //操作日志
                $str = array('aid' => $admin_id, 'content' => '增加知识库第' . $add . '条', 'ctime' => time());
                $record = M('admin_log', '', 'KEFU')->add($str);
                $this->ajax('1001', '添加成功');
            } else {
                $this->ajax('1002', '添加失败');
            }
        }
    }
    /**
     * 删除问题
     */
    public function delknowledge(){
        if(M('knowledge', '', 'KEFU')->where(['id'=>I('id')])->save(['status'=>1])){
            $this->ajax('1001', '删除成功');
        }else{
            $this->ajax('1002', '删除失败');
        }
    }

    /**
     * 知识库列表
     */
    public function knowledgelist(){
        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $knowledge = M('knowledge', '', 'KEFU');
        $search = I('get.know');
        if(!empty(I('get.type'))){
            $where['type']=I('type');
        }
        $this->type=I('get.type');
        if (!empty($search)) {
            $where['standard_question'] = array('like', '%' . $search . '%');
        }
        $where['status']=0;
        $tol = $knowledge->where($where)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $data = $knowledge->LIMIT($page->listfirst, $page->listRows)->field('id,type,standard_question,ctime')->where($where)->order('id desc')->select();
        foreach ($data as $key=>$value){
            switch ($value['type']){
                case 1:$data[$key]['type']='产品使用';break;
                case 2:$data[$key]['type']='支付';break;
                case 3:$data[$key]['type']='认证';break;
                case 4:$data[$key]['type']='基础信息';break;
                case 5:$data[$key]['type']='其他';break;
            }
            if(!empty($value['ctime'])){
                $data[$key]['ctime']=date('Y-m-d H:i',$value['ctime']);
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->page=$page;
        $this->standard_question=$search;
        $this->display();
    }

    public function kno_list_details($id){
        $content=M('knowledge', '', 'KEFU')->where(['id'=>$id])->field('id,admin_id',true)->find();
        $content['time']=date('Y-m-d H:i',$content['ctime']);
        $img=explode(',',$content['img']);
        switch ($content['type']){
            case 1:$content['type']='产品使用';break;
            case 2:$content['type']='支付';break;
            case 3:$content['type']='认证';break;
            case 4:$content['type']='基础信息';break;
            case 5:$content['type']='其他';break;
        }
        $this->assign(array(
            'content' => $content,
            'img_url'=>$img
        ));
//        $this->page=$page;
        $this->display();
    }

    /**
     *上传图片
     */

    public function uploadimg(){
        if (empty($_FILES)) {
            $this->ajaxReturn(['code' => '1002', 'result' => '无图']);
        } else {
            $image=$_FILES['uploadimg'];
            $url='http://gxxj.oss-cn-hangzhou.aliyuncs.com';
            $success = new \Service\OssClient('LTAI6sZcsEa0SGHc','053o9lXKyeLh1J1nmlykMg0z8N0Q5E',$url);
            $ext = strtolower(trim(substr($image['name'], strrpos($image['name'], '.')), '.'));

            $image['name'] = md5(time() . uniqid());
            $object = 'adm/'.$image['name'].'.'.$ext;
            $filePath = $image['tmp_name'];
            try {
                $success->uploadFile('gxxj',$object,$filePath);
                $res= $url.'/adm/'.$image['name'].'.'.$ext;
                $data['pic']=$res;
            } catch (Exception $e) {
                printf(__FUNCTION__ . ": FAILED\n");
                printf($e->getMessage() . "\n");
                $res= '1003';
            }

            if($res=='1002'){
                $data['code']='1002';
                $data['result']='图片不得大于2M';
            }else{
                $data['code']='1001';
                $data['result']=$res;
            }
            $this->ajaxReturn($data);
        }
    }

    public function ajax($code, $result, $r = '')
    {
        if (empty($r)) {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
        } else {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
            return false;
        }
    }
}